<template>
	<span>
		<el-button
		:size="size" :type="type" :icon="icon" :plain="isPlain" @click="handleClick" :loading="loading"><slot></slot>
		</el-button>
		
		<el-dialog title="提示" :visible.sync="dialogVisible" width="30%" >
			
			<el-form :model="memberForm" ref="memberForm" label-width="120px" style="width: 90%;">
			      <el-form-item 
			        label="审核说明" 
			        prop="remark"
			        >
			        <el-input v-model="memberForm.checkStatusExplain" type="textarea" :rows="3"></el-input>
			      </el-form-item>
			</el-form>
			
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="handleOk">确 定</el-button>
			</span>
		</el-dialog>
	</span>
</template>

<script>
import Http from '../../kit/HttpKit';
import Api from '../../api/Api';

export default {
	data() {
		return {
			loading: false,
			dialogVisible: false,
			memberForm: {
				checkStatusExplain: ''
			}
		};
	},
	computed: {
		isPlain: function() {
			if (this.plain == undefined) {
				return true;
			}
			return this.plain;
		}
	},
	props: ['size', 'type', 'icon', 'plain', 'postOption'],
	methods: {
		handleOk(){
			this.dialogVisible = false;
			let obj = this.postOption;
			obj.data.checkStatusExplain = this.memberForm.checkStatusExplain;
			this.doPost(obj);
		},
		handleClick() {
			let obj = this.postOption;
			this.dialogVisible = true;
			// if (obj.confirm != undefined && obj.confirm != '') {
			// 	this.$confirm(obj.confirm, '提示', {
			// 		confirmButtonText: '确定',
			// 		cancelButtonText: '取消',
			// 		type: 'warning'
			// 	}).then(() => {
			// 		this.doPost(obj);
			// 	});
			// } else {
			// 	this.doPost(obj);
			// }
		},
		doPost(obj) {
			let startTime = new Date().getTime();
			this.loading = true;

			let data = obj.data;
			if (data == undefined || data == '') {
				data = {};
			}
			data.api_token = Api.getApiToken();
			data.api_user_id = Api.getApiUserId();

			Http.post(obj.url, data, json => {
				let curTime = new Date().getTime();
				if (curTime - startTime > 500) {
					this.loading = false;

					Api.getDataCB(json, json => {
						if (obj.callback != undefined) {
							obj.callback(json);
							document.activeElement.blur();
						}
					});
				} else {
					setTimeout(() => {
						this.loading = false;

						Api.getDataCB(json, json => {
							if (obj.callback != undefined) {
								obj.callback(json);
								document.activeElement.blur();
							}
						});
					}, 500 - (curTime - startTime));
				}
			});
		}
	}
};
</script>

<style type="text/css"></style>
